package com.wangjw.springbootdemo.zkdemo;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryNTimes;

/**
 * Created by wangjw on 18/5/31.
 */
public class CuratorOperator {

    public CuratorFramework client = null;
    public static final String zkServerPath = "118.126.91.173:2181";

    public CuratorOperator() {
        RetryPolicy retryPolicy = new RetryNTimes(3, 5000);
        client = CuratorFrameworkFactory.builder()
                .connectString(zkServerPath)
                .sessionTimeoutMs(10000)
                .retryPolicy(retryPolicy)
                .build();
        client.start();
    }

    public void closeZKClient() {
        if (client != null) {
            client.close();
        }
    }

    public static void main(String[] args) throws Exception {
        CuratorOperator operator = new CuratorOperator();
        boolean isZkCuratorStarted = operator.client.isStarted();
        System.out.println("当前客户端状态：" + (isZkCuratorStarted ? "连接中" : "已关闭"));

        Thread.sleep(3000);

        operator.closeZKClient();
        isZkCuratorStarted = operator.client.isStarted();
        System.out.println("当前客户端状态：" + (isZkCuratorStarted ? "连接中" : "已关闭"));
    }
}
